package user

import (
	"regexp"

	_ "github.com/go-sql-driver/mysql"
)

type UserLogin struct {
	Username string `json:"username"`
	Password string `json:"password"`
}

type ChangePassReq struct {
	Username string `json:"username"`
	OldPass  string `json:"oldPassword"`
	NewPass  string `json:"newPassword"`
}

type AddUserInfo struct {
	Username   string `json:"username" validate:"required,checkUserName" reg_error_info:"Incorrect format"`
	Password   string `json:"password"`
	Email      string `json:"email" validate:"omitempty,checkEmail,max=100" reg_error_info:"Incorrect format"`
	Phone      string `json:"phone" validate:"omitempty,checkPhone" reg_error_info:"Incorrect format"`
	Activation int8   `json:"activation" validate:"omitempty,gte=-1,lte=1"`
}

type UserChangeInfo struct {
	Email string `json:"email" validate:"omitempty,checkEmail,max=100" reg_error_info:"Incorrect format"`
	Phone string `json:"phone" validate:"omitempty,checkPhone" reg_error_info:"Incorrect format"`
}

type ListOriginInfoResponse struct {
	TotalCount int                  `json:"total_count"`
	Count      int                  `json:"count"`
	Origins    []OriginInfoResponse `json:"origins"`
}

type OriginInfoResponse struct {
	DomainName   string `json:"domain_name"`
	OriProjectId string `json:"ori_project_id"`
	ProjectId    string `json:"project_id"`
	Region       string `json:"region"`
}

type ListRegions struct {
	Count   int      `json:"count"`
	Regions []string `json:"regions"`
}

type AdminChangeUserInfo struct {
	Id         string `json:"id"`
	Username   string `json:"username"`
	Email      string `json:"email" validate:"omitempty,checkEmail,max=100" reg_error_info:"Incorrect format"`
	Phone      string `json:"phone" validate:"omitempty,checkPhone" reg_error_info:"Incorrect format"`
	Activation int8   `json:"activation" validate:"omitempty,gte=-1,lte=1"`
}

type LoginResponse struct {
	Username      string `json:"username"`
	UserId        string `json:"id"`
	AuthToken     string `json:"auth_token"`
	Activation    int8   `json:"activation"`
	UserType      int8   `json:"user_type"`
	TotalResCount int    `json:"total_res_count"`
}

const (
	STATUS_ACTIVATED   int8 = 1
	STATUS_INACTIVATED int8 = -1
	STATUS_DEFAULT     int8 = 0
)

const (
	Administrator int8 = 9
	Sub_Admin     int8 = 5
	General_User  int8 = 0
)

const (
	MaxRetry = 5
)

type UserResourceConfig struct {
	Id                    string `json:"id"`
	OriProjectId          string `json:"ori_project_id"`
	Username              string `json:"username"`
	UserId                string `json:"user_id" validate:"required,max=64"`
	ProjectId             string `json:"project_id" validate:"required,max=64" reg_error_info:"Incorrect format"`
	DomainName            string `json:"domain_name" validate:"required,max=64" reg_error_info:"Incorrect format"`
	Region                string `json:"region" validate:"required,max=64" reg_error_info:"Incorrect format"`
	KeypairName           string `json:"keypair_name" validate:"required,max=64" reg_error_info:"Incorrect format"`
	AccessKey             string `json:"access_key" validate:"required,max=1024"`
	SecretAccessKey       string `json:"secret_access_key" validate:"required,max=1024"`
	AgencyName            string `json:"agency_name" validate:"omitempty,max=64"`
	AuxProxyPath          string `json:"auxproxy_path" validate:"omitempty,max=1024"`
	BuildScriptPath       string `json:"build_script_path" validate:"omitempty,max=1024"`
	DockerBuildScriptPath string `json:"docker_build_script_path" validate:"omitempty,max=1024"`
	FileStorageRegion     string `json:"file_storage_region" validate:"omitempty,max=1024"`
}
type ReturnResConf struct {
	Total       int                  `json:"total"`
	Count       int                  `json:"count"`
	ResConfList []UserResourceConfig `json:"res_conf_list"`
}
type BindResourceConfig struct {
	UserId       string `json:"user_id" validate:"required,max=64"`
	OriProjectId string `json:"ori_project_id" validate:"required,max=64"`
	DomainName   string `json:"domain_name" validate:"required,max=64"`
}

type UpdateUserResConfig struct {
	Id         string `json:"id"`
	Username   string `json:"username"`
	DomainName string `json:"domain_name" validate:"required,max=64" reg_error_info:"Incorrect format"`
	ProjectId  string `json:"project_id" validate:"required,max=64" reg_error_info:"Incorrect format"`
}

type DeleteUserResConfig struct {
	Id string `json:"id"`
}

type CreateOrganizationBody struct {
	Region       string `json:"region" validate:"required,max=64"`
	Organization string `json:"organization" validate:"swrOrg"`
}

type QueryUserResp struct {
	Id             string `json:"id"`
	UserName       string `json:"username"`
	Email          string `json:"email"`
	Phone          string `json:"phone"`
	LastLogin      string `json:"last_login"`
	UserType       int    `json:"user_type"`
	Activation     int    `json:"activation"`
	TotalResNumber int    `json:"total_res_number"`
}

type ListUserResp struct {
	Total int             `json:"total"`
	Count int             `json:"count"`
	Users []QueryUserResp `json:"users"`
}

// func Build

// go自带regexp 不支持预查，需要使用预查可使用开源regexp2 包
func CheckPasswordFun(passwordStr string) bool {
	// 密码包含大、小写字母、数字，特殊符号的三种
	match, err := regexp.MatchString(`^[a-zA-Z0-9.@$!%*#_~?&^]{8,20}$`, passwordStr)
	if err != nil {
		return false
	}
	if !match {
		return false
	}
	var level = 0
	patterns := []string{`[0-9]+`, `[a-z]+`, `[A-Z]+`, `[.@$!%*#_~?&^]+`}
	for _, pattern := range patterns {
		match, err := regexp.MatchString(pattern, passwordStr)
		if err != nil {
			return false
		}
		if match {
			level++
		}
	}
	return level >= 3
}
